package com.lh.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lh.entity.table.*;
import com.lh.mapper.*;
import com.lh.service.TaskService;
import com.lh.utils.Answer;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class TaskServiceImpl implements TaskService {

    @Resource
    private TaskInfoMapper taskInfoMapper;

    @Resource
    private Task1InfoMapper task1InfoMapper;

    @Resource
    private Task2InfoMapper task2InfoMapper;

    @Resource
    private ProblemSummaryInfoMapper problemSummaryInfoMapper;

    @Resource
    private RectificationInfoMapper rectificationInfoMapper;

    @Resource
    private TicketProblemInfoMapper ticketProblemInfoMapper;

    @Resource
    private DeadlineNodeMapper deadlineNodeMapper;

    @Override
    public Answer<Object> addTask(TaskInfo taskInfo) {
        try{
            Integer summaryId = taskInfo.getSummaryId();
            String name = taskInfo.getName();
            String content = taskInfo.getContent();
            if(summaryId == null){
                return Answer.error("summaryId不能为空");
            }
            if(name == null){
                return Answer.error("name不能为空");
            }
            if(content == null){
                return Answer.error("content不能为空");
            }
            ProblemSummaryInfo problemSummaryInfo = problemSummaryInfoMapper.selectById(summaryId);
            if(problemSummaryInfo == null){
                return Answer.error("问题大类不存在");
            }
            taskInfo.setCreateTime(new Date());
            taskInfoMapper.insert(taskInfo);
            return Answer.ok("添加成功");
        }catch (Exception e){
            e.printStackTrace();
            return Answer.error("添加失败");
        }
    }

    @Override
    public Answer<Object> updateTask(TaskInfo taskInfo) {
        try{
            Integer id = taskInfo.getId();
            if(id == null){
                return Answer.error("id不能为空");
            }
            taskInfoMapper.updateById(taskInfo);
            return Answer.ok("更新成功");
        }catch (Exception e){
            e.printStackTrace();
            return Answer.error("更新失败");
        }
    }

    @Override
    public Answer<Object> deleteTask(TaskInfo taskInfo) {
        try{
            Integer id = taskInfo.getId();
            if(id == null){
                return Answer.error("id不能为空");
            }
            taskInfoMapper.deleteById(id);
            return Answer.ok("删除成功");
        }catch (Exception e){
            e.printStackTrace();
            return Answer.error("删除失败");
        }
    }

    @Override
    public Answer<Object> selectTaskList(TaskInfo taskInfo) {
        try{
            Integer summaryId = taskInfo.getSummaryId();
            if(summaryId == null){
                return Answer.error("id不能为空");
            }
            List<TaskInfo> taskInfos = taskInfoMapper.selectList(new LambdaQueryWrapper<TaskInfo>().eq(TaskInfo::getSummaryId, summaryId));
            return Answer.ok(taskInfos);
        }catch (Exception e){
            e.printStackTrace();
            return Answer.error("查询失败");
        }
    }

    @Override
    public Answer<Object> addTask1(Task1Info task1Info) {
        try{
            Integer taskId = task1Info.getTaskId();
            String name = task1Info.getName();
            String content = task1Info.getContent();
            if(taskId == null){
                return Answer.error("taskId不能为空");
            }
            if(name == null){
                return Answer.error("name不能为空");
            }
            if(content == null){
                return Answer.error("content不能为空");
            }
            TaskInfo taskInfo = taskInfoMapper.selectById(taskId);
            if(taskInfo == null){
                return Answer.error("任务表不存在");
            }
            task1Info.setCreateTime(new Date());
            task1InfoMapper.insert(task1Info);
            return Answer.ok("添加成功");
        }catch (Exception e){
            e.printStackTrace();
            return Answer.error("添加失败");
        }
    }

    @Override
    public Answer<Object> updateTask1(Task1Info task1Info) {
        try{
            Integer id = task1Info.getId();
            if(id == null){
                return Answer.error("id不能为空");
            }
            task1InfoMapper.updateById(task1Info);
            return Answer.ok("更新成功");
        }catch (Exception e){
            e.printStackTrace();
            return Answer.error("更新失败");
        }
    }

    @Override
    public Answer<Object> deleteTask1(Task1Info task1Info) {
        try{
            Integer id = task1Info.getId();
            if(id == null){
                return Answer.error("id不能为空");
            }
            task1InfoMapper.deleteById(id);
            return Answer.ok("删除成功");
        }catch (Exception e){
            e.printStackTrace();
            return Answer.error("删除失败");
        }
    }

    @Override
    public Answer<Object> selectTask1List(Task1Info task1Info) {
        try{
            Integer taskId = task1Info.getTaskId();
            if(taskId == null){
                return Answer.error("taskId不能为空");
            }
            List<Task1Info> task1Infos = task1InfoMapper.selectList(new LambdaQueryWrapper<Task1Info>().eq(Task1Info::getTaskId, taskId));
            return Answer.ok(task1Infos);
        }catch (Exception e){
            e.printStackTrace();
            return Answer.error("查询失败");
        }
    }

    @Override
    public Answer<Object> addTask2(Task2Info task2Info) {
        try{
            Integer task1Id = task2Info.getTask1Id();
            String name = task2Info.getName();
            String content = task2Info.getContent();
            if(task1Id == null){
                return Answer.error("task1Id不能为空");
            }
            if(name == null){
                return Answer.error("name不能为空");
            }
            if(content == null){
                return Answer.error("content不能为空");
            }
            Task1Info task1Info = task1InfoMapper.selectById(task1Id);
            if(task1Info == null){
                return Answer.error("任务1不存在");
            }
            task2Info.setCreateTime(new Date());
            task2InfoMapper.insert(task2Info);
            return Answer.ok("添加成功");
        }catch (Exception e){
            e.printStackTrace();
            return Answer.error("添加失败");
        }
    }

    @Override
    public Answer<Object> updateTask2(Task2Info task2Info) {
        try{
            Integer id = task2Info.getId();
            if(id == null){
                return Answer.error("id不能为空");
            }
            task2InfoMapper.updateById(task2Info);
            return Answer.ok("更新成功");
        }catch (Exception e){
            e.printStackTrace();
            return Answer.error("更新失败");
        }
    }

    @Override
    public Answer<Object> deleteTask2(Task2Info task2Info) {
        try{
            Integer id = task2Info.getId();
            if(id == null){
                return Answer.error("id不能为空");
            }
            task2InfoMapper.deleteById(id);
            return Answer.ok("删除成功");
        }catch (Exception e){
            e.printStackTrace();
            return Answer.error("删除失败");
        }
    }

    @Override
    public Answer<Object> selectTask2List(Task2Info task2Info) {
        try{
            Integer task1Id = task2Info.getTask1Id();
            if(task1Id == null){
                return Answer.error("task1Id不能为空");
            }
            List<Task2Info> task2Infos = task2InfoMapper.selectList(new LambdaQueryWrapper<Task2Info>().eq(Task2Info::getTask1Id, task1Id));
            return Answer.ok(task2Infos);
        }catch (Exception e){
            e.printStackTrace();
            return Answer.error("查询失败");
        }
    }

    @Override
    public Answer<Object> queryTask2Progress(JSONObject reqJson) {
        try{
            JSONObject resJson = new JSONObject();
            Integer id = reqJson.getInteger("id");
            String departmentName = reqJson.getString("departmentName");
            if(id == null){
                return Answer.error("task2Id不能为空");
            }
            Task2Info task2Info = task2InfoMapper.selectById(id);
            if(task2Info == null){
                return Answer.error("任务2不存在");
            }
            LambdaQueryWrapper<RectificationInfo> rectificationInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
            rectificationInfoLambdaQueryWrapper.eq(RectificationInfo::getTask2Id, id);
            RectificationInfo rectificationInfo = rectificationInfoMapper.selectOne(rectificationInfoLambdaQueryWrapper);
            if (rectificationInfo != null){
                Integer id2 = rectificationInfo.getId();
                List<DeadlineNode> deadlineNodeList = deadlineNodeMapper.selectList(new LambdaQueryWrapper<DeadlineNode>().eq(DeadlineNode::getMainId, id2).eq(DeadlineNode::getTypeName, "整改").orderByAsc(DeadlineNode::getSort));
                rectificationInfo.setDeadlineNodeList(deadlineNodeList);
            }

            LambdaQueryWrapper<TicketProblemInfo> ticketProblemInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
            ticketProblemInfoLambdaQueryWrapper.eq(TicketProblemInfo::getTask2Id, id);
            if (StrUtil.isNotBlank(departmentName)){
                ticketProblemInfoLambdaQueryWrapper.eq(TicketProblemInfo::getProcessHandlerDepartment, departmentName);
            }
            List<TicketProblemInfo> ticketProblemInfoList = ticketProblemInfoMapper.selectList(ticketProblemInfoLambdaQueryWrapper);
            if (CollectionUtil.isNotEmpty(ticketProblemInfoList)){
                for (TicketProblemInfo ticketProblemInfo : ticketProblemInfoList) {
                    Integer id1 = ticketProblemInfo.getId();
                    List<DeadlineNode> deadlineNodeList = deadlineNodeMapper.selectList(new LambdaQueryWrapper<DeadlineNode>().eq(DeadlineNode::getMainId, id1).eq(DeadlineNode::getTypeName, "下发").orderByAsc(DeadlineNode::getSort));
                    ticketProblemInfo.setDeadlineNodeList(deadlineNodeList);
                }
            }
            resJson.put("rectificationInfo", rectificationInfo);
            resJson.put("ticketProblemInfoList", ticketProblemInfoList);
            return Answer.ok(resJson);
        }catch (Exception e){
            e.printStackTrace();
            return Answer.error("查询失败");
        }
    }
}
